/**
* Copyright (c) 2016-2100 yunclouds
*/
package com.yunqian.redot.service;

import com.yunqian.redot.constant.BooleanStatus;
import com.yunqian.redot.domain.Menu;
import com.yunqian.redot.domain.dto.MenuDto;
import com.yunqian.redot.repository.MenuRepository;
import com.yunqian.service.BaseService;
import com.yunqian.util.bean.BeanUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author czy
 * 2018-11-08 09:42:03
 */
@Log4j2
@Service
public class MenuService extends BaseService<Menu, Long> {

    @Autowired
    private MenuRepository menuRepository;

    public List<MenuDto> getAllMenu(){

        // 一级菜单
        List<Menu> firstLevel = menuRepository.findAllByStatusAndParentIdIsNull(BooleanStatus.YES.getCode());
        // 二级菜单
        List<Menu> secondLevel = menuRepository.findAllByStatusAndUrlIsNotNull(BooleanStatus.YES.getCode());
        List<MenuDto> dtoList = new ArrayList<>();
        firstLevel.forEach(item -> {
            MenuDto dto = new MenuDto(new Menu());
            BeanUtil.copyNotNullProperties(dto.getMenu(), item);
            List<Menu> menuList = new ArrayList<>();
            secondLevel.forEach(menu -> {
                if(item.getId().equals(menu.getParentId())){
                    menuList.add(menu);
                }
            });
            dto.setMenuList(menuList);
            dtoList.add(dto);
        });
        return dtoList;
    }

}
